import { ref } from 'vue'

export default function () {
  const visible = ref(false)

  const openPopover = (callback: any = '') => {
    if (visible.value) {
      closePopover()
      return
    }

    visible.value = true
    // eslint-disable-next-line no-eval
    callback && eval(callback)
    setTimeout(() => {
      document.addEventListener('click', closePopover)
    }, 100)
  }

  const closePopover = (callback: any = '') => {
    visible.value = false
    // eslint-disable-next-line no-eval
    callback && eval(callback)
    document.removeEventListener('click', closePopover)
  }

  return {
    visible,
    openPopover,
    closePopover
  }
}
